//
//  RankUserCell.swift
//  WalkNovel
//
//  Created by Kevin on 2021/11/8.
//

import UIKit

class RankUserCell: UITableViewCell {
    
    lazy var numLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .regular, size: 14)
        label.textAlignment = .center
        label.textColor = .black
        return label
    }()
    
    lazy var headImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "placeholder_header"))
        image.contentMode = .scaleAspectFill
        image.wn_cornerWithBorder(27)
        return image
    }()
    
    lazy var numImage: UIImageView = {
        return UIImageView()
    }()
    
    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.textColor = .black
        label.font = .wn_font(name: .bold, size: 14)
        return label
    }()
    
    lazy var readLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 11)
        label.textColor = .init(named: "gray_999999")
        return label
    }()
    
    lazy var nowLabel: UILabel = {
        let label = UILabel()
        label.textColor = .init(named: "red_FE95B4")
        label.font = .wn_font(name: .medium, size: 12)
        label.numberOfLines = 0
        label.text = "最近\n讀過\n書籍"
        return label
    }()
    
    lazy var scrollView: UIScrollView = {
        let scro = UIScrollView.init(frame: .zero)
        scro.showsHorizontalScrollIndicator = true
        scro.showsVerticalScrollIndicator = false
        return scro
    }()
    
    var bookViewArr = [RankUserBookView]()

    var bookArr: [BookDetailModel]? {
        didSet {
            if let arr = bookArr, arr.count != 0 {
                for item in scrollView.subviews {
                    item.removeFromSuperview()
                }
                bookViewArr.removeAll()
                let showBookArr = arr.count > 1 ? arr.sample(size: 1) : arr
                for i in 0..<(showBookArr?.count ?? 0) {
                    let view = RankUserBookView()
                    view.frame = CGRect(x: CGFloat(i)*(scaleWidth(40)+6), y: 0, width: (scaleWidth(40)), height: scaleWidth(58)+16)
                    view.model = showBookArr?[i]
                    bookViewArr.append(view)
                    scrollView.addSubview(view)
                }
                scrollView.contentSize = CGSize(width: CGFloat(showBookArr?.count ?? 0)*(scaleWidth(40)+6), height: 0)
            }
        }
    }
    
    var model: BookDetailModel? {
        didSet {
            if let m = model {
                headImage.wn_setupImg(m.avatar, placeHolder: "placeholder_header")
                nameLabel.text = m.userName ?? "用戶"
                readLabel.text = "讀過 \(m.num) 本書"
                bookArr = [m, m, m, m, m, m]
            }
        }
    }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .white
        contentView.addSubview(numLabel)
        contentView.addSubview(headImage)
        contentView.addSubview(numImage)
        contentView.addSubview(nameLabel)
        contentView.addSubview(readLabel)
        contentView.addSubview(scrollView)
        contentView.addSubview(nowLabel)
        numLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(12)
        }
        headImage.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(36)
            make.width.height.equalTo(54)
        }
        numImage.snp.makeConstraints { make in
            make.top.equalTo(headImage.snp.top).offset(-6)
            make.right.equalTo(headImage.snp.right).offset(-4)
            make.width.height.equalTo(14)
        }
        nameLabel.snp.makeConstraints { make in
            make.top.equalTo(headImage.snp.top).offset(2)
            make.left.equalTo(headImage.snp.right).offset(12)
            make.width.lessThanOrEqualTo(scaleWidth(120))
        }
        readLabel.snp.makeConstraints { make in
            make.top.equalTo(nameLabel.snp.bottom).offset(12)
            make.left.equalTo(nameLabel.snp.left)
        }
        scrollView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.width.equalTo(scaleWidth(40)+6)
            make.right.equalTo(-12)
            make.height.equalTo(scaleWidth(58))
        }
        nowLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(scrollView.snp.left).offset(-6)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class RankUserBookView: UIView {
    
    lazy var bookBtn: UIButton = {
         let btn = UIButton(type: .custom)
        btn.rx.tap.bind { [weak self] in
            self?.viewContainingController?.wn_pushBookVC(self?.model?.bookId ?? "")
        }.disposed(by: rx.disposeBag)
        btn.imageView?.contentMode = .scaleAspectFill
        btn.imageView?.wn_cornerWithBorder(2)
        btn.imageView?.clipsToBounds = true
        return btn
    }()
    
    var model: BookDetailModel? {
        didSet {
            if let m = model {
                bookBtn.wn_setupImg(m.coverUrl, placeHolder: "placeholder_cover_ver")
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(bookBtn)
        bookBtn.snp.makeConstraints { (make) in
            make.top.left.right.equalTo(0)
            make.size.equalTo(CGSize(width: scaleWidth(40), height: scaleWidth(58)))
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
